using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Windows.Forms;

namespace theguineasyndrome
{

    public class TheGuineaSyndromeGame : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;

        private AppMode appMode;

        private int lastGcElapsedTime;

        public TheGuineaSyndromeGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            IsMouseVisible = true;
            
#if true
            DisplayMode displayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;

            graphics.PreferredBackBufferWidth = displayMode.Width;
            graphics.PreferredBackBufferHeight = displayMode.Height;

            graphics.IsFullScreen = true;

#else
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 600;
            Window.AllowUserResizing = true;
            Window.ClientSizeChanged += new EventHandler<EventArgs>(Window_ClientSizeChanged);
#endif

            graphics.SynchronizeWithVerticalRetrace = true;
        }

        public void setMode(AppMode appMode)
        {
            appMode.LoadContent();
            appMode.Initialize();

            this.appMode = appMode;
        }

        private void Window_ClientSizeChanged(object sender, EventArgs e)
        {
            GameWindow window = (GameWindow) sender;
            appMode.notifyResize(new Vector2(window.ClientBounds.Width, window.ClientBounds.Height));
        }

        protected override void Initialize()
        {
            base.Initialize();

            lastGcElapsedTime = 0;
            appMode.Initialize();
        }

        protected override void LoadContent()
        {
#if true
            appMode = new MenuMode(this, GraphicsDevice, Content, Window);
#else
            appMode = new EditorMode(this, GraphicsDevice, Content, Window);
#endif

            appMode.LoadContent();
        }

        protected override void UnloadContent()
        {
            
        }

        protected override void Update(GameTime gameTime)
        {
            appMode.Update(gameTime);

            base.Update(gameTime);

            // run gc each second
            lastGcElapsedTime += (int)gameTime.TotalGameTime.TotalMilliseconds;
            if (lastGcElapsedTime < 1000)
            {
                lastGcElapsedTime = 0;
                System.GC.Collect();
            }
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.LightGray);

            appMode.Draw(gameTime);

            base.Draw(gameTime);
        }
    }
}
